﻿using System;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.ServiceProcess;

namespace AccessBasedDesktop.Service
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
        }

        public static void StartService(string serviceName, int timeoutMilliseconds)
        {
            var service = new ServiceController(serviceName);
            var timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

            service.Start();
            service.WaitForStatus(ServiceControllerStatus.Running, timeout);
        }

        public void ChangeServiceAccount(String serviceName, String user, String password)
        {
            var startInfo = new ProcessStartInfo("sc.exe");

            startInfo.Arguments = String.Format("config {0} obj= {1} password= {2}", serviceName, user, password);

            startInfo.RedirectStandardOutput = true;

            startInfo.UseShellExecute = false;

            using (var p = new Process())
            {
                p.StartInfo = startInfo;

                if (p.Start())
                {
                    p.WaitForExit();
                }
            }
        }
    }
}